package com.skit.common.adapter

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.isGone
import androidx.databinding.BindingAdapter
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.skit.common.R
import com.skit.common.bean.User

@BindingAdapter("isGone")
fun isGone(view: View, boolean: Boolean) {
    view.visibility = if (boolean) View.GONE else View.VISIBLE
}

@BindingAdapter("userIcon")
fun userIcon(view: ImageView, url: String?) {
    Glide.with(view)
        .load(url ?: "file:///android_asset/batman.jpg")
        .into(view)
}

@BindingAdapter("userCircleIcon")
fun userCircleIcon(view: ImageView, url: String?) {
    val requestOption = RequestOptions.circleCropTransform()
    Glide.with(view)
        .load(url ?: "file:///android_asset/batman.jpg")
        .apply(requestOption)
        .into(view)
}

@BindingAdapter("userSex")
fun userSex(view: TextView, user: User?) {
    val sexDrawableArrays =
        arrayOf(R.drawable.ic_sex_unknown, R.drawable.ic_man, R.drawable.ic_woman)
    view.setCompoundDrawablesWithIntrinsicBounds(
        null,
        null,
        view.context.resources.getDrawable(
            sexDrawableArrays[user?.userInfo?.sex ?: 0],
            view.context.theme
        ),
        null
    )
}
/*
@BindingAdapter("urlImage", "circle")
fun loadImage(view: ImageView, url: String?, circle: Boolean = false) {
    val requestOption = RequestOptions.circleCropTransform();
    if (circle) {
        Glide.with(view)
            .load(url ?: "file:///android_asset/batman.jpg")
            .apply(requestOption)
            .into(view)
    } else {
        Glide.with(view)
            .load(url ?: "file:///android_asset/batman.jpg")
            .into(view)
    }
}*/
